<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

define('IN_ADMIN_COMPONENT_CONTROLLER', 'true');

require_once('includes/functions/configuration_functions.php');

class ReviewsComponent
{
	function ReviewsComponent()
	{
	}

	function action(&$params, $action)
	{
		if ($action == 'Update') 
			return $this->edit($params);
		else
			return $this->query($params);
	}

	function query(&$params) 
	{
		$handle = get_class($this);

		$languageId = (int)$params['languageId'];

		$paginate = (boolean)$params['paginate'];
		$max_rows = (int)$params['max_rows'];
		$offset = (int)$params['offset'];

		$subParams['handle'] = $handle;
		$subParams['languageId'] = $languageId;
		$subParams['max_rows'] = $max_rows;
		$subParams['offset'] = $offset;
		$subParams['searchReviews'] = true;
		$subParams['return_page_info'] = $paginate;

		require_once('includes/components/ProductsSearchSubComponent.php');
		$subComponent = new ProductsSearchSubComponent();
		$value = $subComponent->query($subParams);


		return $value;
	}

	function edit(&$params) 
	{

		$languageId = (int)$params['languageId'];
		$i = 0;
		while (true)
		{
			$i++;
			if ($i > 1000) break;

			$reviewId = (int)$params['reviewId-'.$i];
			if ($reviewId < 1) break;

			$note = prepare_input($params['reviewNote-'.$i]);

			$deleteRow = (boolean)$params['reviewDelete-'.$i];

			if ($deleteRow) {
				if ($reviewId < 1) continue;
				$sql = 'delete from ' . TABLE_REVIEWS . ' where reviews_id=' . $reviewId;
				mysql_query($sql) or die(mysql_error());
				$sql = 'delete from ' . TABLE_REVIEWS_DESCRIPTION . ' where reviews_id=' . $reviewId;
				mysql_query($sql) or die(mysql_error());
			}
			else if ($note != null && strlen($note) > 0) {
				$text = '';
				$sqlQuery = 'select reviews_text from ' . TABLE_REVIEWS_DESCRIPTION . ' where reviews_id=' . $reviewId;
				$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
				$cnt = 0;
				while($row = mysql_fetch_array($dataReturned))
				{
					$text = $row['reviews_text'];
					break;
				}

				$sql_data_array = array();
				$sqlAction = 'update';
				$where = 'reviews_id=' . $reviewId;
				$sql_data_array['last_modified'] = 'now()';
				db_perform(TABLE_REVIEWS, $sql_data_array, $sqlAction, $where);

				$sql_data_array = array();
				$sqlAction = 'update';
				$where = 'reviews_id=' . $reviewId;
				$sql_data_array['reviews_text'] = $text . '<br/>owner note: ' . $note;
				db_perform(TABLE_REVIEWS_DESCRIPTION, $sql_data_array, $sqlAction, $where);
			}
		}

		return $this->query($params);
	}
}
?>
